//定义js命名空间
Ext.namespace('FSBS.FsbsQmStrc');

//FsbsQmStrc FieldArray
FSBS.FsbsQmStrc.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'DataId', mapping: 'Id' },
	{ name: 'QMInout', type: 'string' },
	{ name: 'QMType', type: 'string' },
	{ name: 'ProviderId', type: 'string' },
	{ name: 'DepartmentId', type: 'string' },
	{ name: 'OperuserId', type: 'string' },
	{ name: 'CheckuserId', type: 'string' },
	{ name: 'QMState', type: 'int' },
	{ name: 'Description', type: 'string' },
	{ name: 'Isdelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'Createtime', type: 'DateTime' }
);

//FsbsQmStrc JsonReader
FSBS.FsbsQmStrc.JsonReader_OO = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmStrc.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FSBS.FsbsQmStrc.BaseFieldsArray
		}, cfg));
	}
});

//FsbsQmStrc JsonStore
FSBS.FsbsQmStrc.JsonStore_Old = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FSBS.FsbsQmStrc.JsonStore_Old.superclass.constructor.call(this, Ext.applyIf({
			QryUrl: '/FsbsQmStrc/GetPlist', //查询Url
			UpdUrl: '/FsbsQmStrc/SaveOrUpdate', //更新记录Url
			DelUrl: '/FsbsQmStrc/Delete', //删除记录Url
			fields: FSBS.FsbsQmStrc.BaseFieldsArray,
			sortInfo: {
				field: 'CreateTime',
				direction: 'DESC' // or 'DESC' (case sensitive for local sorting)
			}
		}, cfg));
	}
});

//FsbsQmStrc JsonStore
FSBS.FsbsQmStrc.JsonStore_Old = Ext.extend(Infrastructure.BaseJsonStore, {
	url: '/FsbsQmStrc/GetPlist', //查询Url
	UpdUrl: '/FsbsQmStrc/SaveOrUpdate', //更新记录Url
	DelUrl: '/FsbsQmStrc/Delete', //删除记录Url
	fields: FSBS.FsbsQmStrc.BaseFieldsArray,
	sortInfo: {
		field: 'CreateTime',
		direction: 'DESC' // or 'DESC' (case sensitive for local sorting)
	},
	initComponent: function () {
		FSBS.FsbsQmStrc.JsonStore.superclass.initComponent.call(this);
	}
});

//FsbsQmStrc JsonStore
FSBS.FsbsQmStrc.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	url: '/FsbsQmStrc/GetPlist', //查询Url
	UpdUrl: '/FsbsQmStrc/SaveOrUpdate', //更新记录Url
	DelUrl: '/FsbsQmStrc/Delete', //删除记录Url
	fields: FSBS.FsbsQmStrc.BaseFieldsArray,
	sortInfo: {
		field: 'CreateTime',
		direction: 'DESC' // or 'DESC' (case sensitive for local sorting)
	},
	initComponent: function () {
		FSBS.FsbsQmStrc.JsonStore.superclass.initComponent.call(this);
	}
});

//FsbsQmStrc GridPanel
FSBS.FsbsQmStrc.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	qmInOut: 'qmio_0', //入库
	//基本处理Url
	baseUrl: '/FsbsQmStrc/',
	//提交保存Url
	formSubmitUrl: '/FsbsQmStrc/SaveOrUpdate',

	//constructor
	initComponent: function () {
		//prepare
		var ioTypeName = this.qmInOut == 'qmio_0' ? '入库' : '出库';
		var itemGrid = this;
		this.menuPId = this.qmInOut == 'qmio_0' ? '0101' : '0102';

		// default store
		var gridStore = new FSBS.FsbsQmStrc.JsonStore();
		gridStore.setBaseParam('qmInOut', this.qmInOut);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [{
				dataIndex: 'QMInout',
				type: 'list',
				options: Infrastructure.Params.DictFilter('QMInout')
			}, {
				dataIndex: 'QMType',
				type: 'list',
				options: Infrastructure.Params.DictFilter(this.qmInOut)
			}, {
				dataIndex: 'ProviderId',
				type: 'string'
			}, {
				dataIndex: 'DepartmentId',
				type: 'list',
				options: Infrastructure.Params.DepartmentFilter()
			}, {
				dataIndex: 'OperuserId',
				name: 'Operuser.Name',
				type: 'string'
			}, {
				dataIndex: 'CheckuserId',
				type: 'string'
			}, {
				dataIndex: 'QMState',
				type: 'list',
				options: Infrastructure.Params.KeyValueFilter(FSBS.Params.QmStateStore)
			}]
		});

		// selModel
		var gridSelModel = new Ext.grid.CheckboxSelectionModel({
			header: '',
			singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					var bottomToolbar = itemGrid.getBottomToolbar();
					bottomToolbar.btnDel.setDisabled(sm.getCount() < 1);
					var s = itemGrid.getSelectionModel().getSelections();
					for (var i = 0, r; r = s[i]; i++) {
						if (r.data.QMState != 0) {
							bottomToolbar.btnDel.setDisabled(true);
							break;
						}
					}
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				gridSelModel,
				{
					header: ioTypeName + '单编号',
					dataIndex: 'Id'
				}, {
					header: "出入库",
					dataIndex: 'QMInout',
					editor: new Infrastructure.Params.InfraDictComboBox({
						filterValue: 'QMInout'
					}),
					xtype: 'infra_dictcolumn',
					dictCode: 'QMInout',
					editable: false
				}, {
					header: "分类",
					dataIndex: 'QMType',
					xtype: 'infra_dictcolumn',
					dictCode: this.qmInOut,
					editable: true
				}, {
					header: this.qmInOut == 'qmio_0' ? '供应商' : '出库客户',
					dataIndex: 'ProviderId',
					editable: true
				}, {
					header: "机构",
					dataIndex: 'DepartmentId',
					xtype: 'infra_deptcolumn'
				}, {
					header: "经办人",
					dataIndex: 'OperuserId',
					xtype: 'infra_usercolumn'
				}, {
					header: "复核人",
					dataIndex: 'CheckuserId',
					xtype: 'infra_usercolumn'
				}, {
					header: "状态",
					dataIndex: 'QMState',
					xtype: 'infra_KeyValueColumn',
					dataStore: FSBS.Params.QmStateStore
				}
			],
			defaults: {
				sortable: true,
				editable: false,
				editor: {
					xtype: 'textfield'
				}
			}
		});

		// 常用工具栏
		var toolBar = [{
			menuid: itemGrid.qmInOut == 'qmio_0' ? '010103' : '010203',
			xtype: 'tbarmenubtn',
			ref: '../stoBtn',
			text: ioTypeName,
			icon: '/Content/icons/accept.png',
			handler: function () {
				submitUrl = itemGrid.qmInOut == 'qmio_0' ? 'StorageIn' : 'StorageOut';
				itemGrid.tbarSubmit(itemGrid.baseUrl + submitUrl, '确定要' + ioTypeName + '吗?', '车辆信息' + ioTypeName + '成功。');
			}
		}];

		Ext.applyIf(this, {
			plugins: [gridFilter],
			store: gridStore,
			sm: gridSelModel,
			cm: gridColumn,
			tbar: toolBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					//var sRecord = grid.store.getAt(rowIndex);
					this.showEntity(0, grid.store.getAt(rowIndex));
					//this.showCarListTest(grid.store.getAt(rowIndex));
				},
				'onAddNewEntity': function (newid, rt) {
					if (rt) {
						this.onAddEntity(newid);
					}
				},
				'onShowEntity': function (newid, rt) {
					if (rt) {
						this.showEntity(newid);
					}
				}
			}
		});

		FSBS.FsbsQmStrc.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	//bbar添加按钮
	addEntity: function () {
		//this.showEntity();
		this.addNewEntity('CS', '', 'onShowEntity');
		//this.addNewEntity('CS', '', 'onAddNewEntity');
	},

	showEntity: function (itemId, sRecord) {
		var itemGrid = this;

		var carStore = new FSBS.FsbsQmCrif.JsonStore({
			QryUrl: '/FsbsQmStrc/GetCarPlist', //查询Url
			UpdUrl: '/FsbsQmStrc/SaveOrUpdateCarInfo', //更新记录Url
			DelUrl: '/FsbsQmStrc/DeleteCar?storeid=' + sRecord.data.Id //删除记录Url
		});

		var carGrid = new FSBS.FsbsQmCrif.GridPanel({
			store: carStore,
			height: 400
		});

		var itemEditForm = new FSBS.FsbsQmStrc.EditForm({
			dataRecord: sRecord
		});

		var itemWin = new Infrastructure.BaseWindow({
			title: '库存信息',
			iconCls: 'car',
			closeAction: 'hide',
			width: 850,
			items: [carGrid, itemEditForm],

			buttons: [{
				xtype: 'btnsubmit',
				menuid: '010101',
				handler: function () {
					itemEditForm.saveItem();
				},
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemWin.hide();
				}
			}]
		});

		itemWin.show();
		carStore.load({
			params: {
				storeid: sRecord.data.Id
			}
		});
	},

	showEntity_Test: function (grid, rowIndex) {
		var custGrid = new Infrastructure.CifCust.GridPanel({
			menuPId: '0000',
			height: 500,
			listeners: {
				'afterChoose': function (selRecord, rt) {
					if (rt) {
						tgField.setValue(selRecord.data.Id);
						custGridWin.destroy(); //关闭窗口
						tgField.fireEvent('afterSelect', tgField, selRecord, true);
					}
				}
			}
		});
		var custGridWin = new Infrastructure.BaseWindow({
			title: '请选择客户',
			iconCls: 'car',
			closeAction: 'hide',
			width: 800,
			items: [custGrid]
		});
		custGridWin.show();
		custGrid.store.load();
	},

	// 添加
	onAddEntity: function (newId) {
		var itemGrid = this;
		var itemEditForm = new FSBS.FsbsQmStrc.EditForm({
			dataId: newId,
			qmInOut: itemGrid.qmInOut,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						//ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						//JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		var itemEditWin = new FSBS.FsbsQmStrc.EditWin({
			title: itemGrid.qmInOut == 'qmio_0' ? 'FSBS_汽贸管理_商品车入库单' : 'FSBS_汽贸管理_商品车出库单',
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	showCarListTest: function (selRecord) {
		var carGrid = new FSBS.FsbsQmCrif.GridPanel({
			height: 500
		});
		var itemWin = new Infrastructure.BaseWindow({
			title: '库存信息',
			iconCls: 'car',
			closeAction: 'hide',
			width: 800,
			items: [carGrid]
		});
		itemWin.show();
		carGrid.store.load();
	},

	// 读取信息显示
	showCarListTest_bad: function () {
		//var itemGrid = this;
		//var selRecord = this.getSelectionModel().getSelected();
		//if (selRecord == null) {
		//	JsHelper.ShowError('请选择记录编辑..');
		//	return -1;
		//}
		//var selId = selRecord.data.Id;
		//alert('abc');
		/*
		var itemEditForm = new FSBS.FsbsQmStrc.EditForm({
		qmInOut: itemGrid.qmInOut,
		dataId: selId,
		dataRecord: selRecord,
		submitUrl: this.formSubmitUrl,
		submitMsg: this.formSubmitMsg,
		waitMsg: '数据加载中...',
		listeners: {
		'afterSave': function (ele, rt) {
		if (rt) {
		itemGrid.store.reload();
		JsHelper.ShowInfo('信息更新成功!');
		}
		}
		}
		});
		var carStore = new Infrastructure.BaseJsonStore({
		QryUrl: '/FsbsQmStrc/GetCarPlist', //查询Url
		UpdUrl: '/FsbsQmStrc/SaveOrUpdateCarInfo', //更新记录Url
		DelUrl: '/FsbsQmStrc/DeleteCar?storeid=' + this.dataId, //删除记录Url
		fields: FSBS.FsbsQmCrif.BaseFieldsArray
		});

		carStore.setBaseParam('storeid', selId);
		*/
		var carGrid = new FSBS.FsbsQmCrif.GridPanel({
			//baseUrl: '/FsbsQmStrc/',
			//提交保存Url
			//formSubmitUrl: '/FsbsQmStrc/SaveOrUpdateCarInfo?storeid=' + selId,
			//storeId: selId,
			height: 400
			/*,
			tbar: [{
			xtype: 'tbaraddbtn', //添加入库车辆
			menuid: '010101',
			//hidden: !(isEdit && qmio == 'qmio_0'),
			handler: function () {
			carGrid.onShowItemWin();
			}
			}, {
			xtype: 'tbardelbtn', //删除入库车辆
			menuid: '010101',
			//hidden: !(isEdit && qmio == 'qmio_0'),
			handler: function () {
			carGrid.delEntity();
			}
			}, {
			xtype: 'tbaraddbtn', //添加出库车辆
			menuid: '010201',
			//hidden: !(isEdit && qmio == 'qmio_1'),
			text: '添加出库车辆',
			handler: function () {
			carGrid.chooseItem();
			}
			}, {
			xtype: 'tbardelbtn', //删除出库车辆
			menuid: '010201',
			//hidden: !(isEdit && qmio == 'qmio_1'),
			handler: function () {
			carGrid.delEntity();
			}
			}, {
			xtype: 'tbareditbtn', //编辑车辆信息
			menuid: '010101',
			hidden: true,
			handler: function () {
			carGrid.onShowItemWin(itemGrid.getSelectionModel().getSelected());
			}
			}]
			/*,
			chooseItem: function () {
			var carStoreGrid = new FSBS.FsbsQmCrif.GridPanel({
			carState: '1',
			height: 500,
			tbar: [{
			xtype: 'tbaraddbtn',
			text: '选择',
			handler: function (e) {
			carStoreGrid.tbarSubmit('/FsbsQmStrc/StoreOutAddCars?StoreId=' + carGrid.storeId, '选定车辆添加到出库单吗?', '添加成功', 'afterChoose');
			}
			}],
			listeners: {
			'afterChoose': function (ele, rt) {
			if (rt) {
			carGrid.store.reload();
			}
			}
			}
			});
			var carStoreGridWin = new Infrastructure.BaseWindow({
			title: '请选择车辆',
			iconCls: 'car',
			width: 600,
			items: [carStoreGrid]
			});
			carStoreGridWin.show();
			carStoreGrid.store.load();
			}*/
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			//title: selRecord.data['QMInout'] == 'qmio_0' ? 'FSBS_汽贸管理_商品车入库单' : 'FSBS_汽贸管理_商品车出库单',
			//items: [itemEditForm, carGrid]
			items: [carGrid]
		});
		itemEditWin.show();
		carGrid.store.load();
		//itemEditForm.form.loadRecord(selRecord);
	}
});

//FsbsQmStrc 汽贸入库单
FSBS.FsbsQmStrc.StoreInPanel = Ext.extend(Infrastructure.BasePanel, {
	dataId: null,
	qmInOut: 'qmio_0', //入库
	initComponent: function () {
		Ext.applyIf(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .33,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					items: [{
						fieldLabel: "编号",
						xtype: 'textfield',
						width: 150,
						readOnly: true,
						value: this.dataId,
						name: 'DataId'
					}, {
						fieldLabel: "分类",
						xtype: 'infra_dictcombo',
						hiddenName: 'QMType',
						filterValue: this.qmInOut
					}, {
						fieldLabel: this.qmInOut == 'qmio_0' ? '供应商' : '客户',
						width: 150,
						maxLength: 36,
						xtype: 'textfield',
						name: 'ProviderId'
					}]
				}, {
					columnWidth: .66,
					layout: 'form',
					bodyStyle: 'padding:1px',
					items: [{
						layout: 'column',
						items: [{
							columnWidth: .5,
							bodyStyle: 'padding:1px',
							layout: 'form',
							items: [{
								xtype: 'infra_deptcombo',
								//readOnly: true,
								hiddenName: 'DepartmentId'
							}, {
								fieldLabel: "状态",
								xtype: 'infra_KeyValueCombo',
								//readOnly: true,
								hiddenName: 'QMState',
								store: FSBS.Params.QmStateStore
							}]
						}, {
							columnWidth: .5,
							bodyStyle: 'padding:1px',
							layout: 'form',
							items: [{
								fieldLabel: "经办人",
								//readOnly: true,
								xtype: 'infra_usercombo',
								hiddenName: 'OperuserId'
							}, {
								fieldLabel: "复核人",
								//readOnly: true,
								allowBlank: true,
								emptyText: '',
								xtype: 'infra_usercombo',
								hiddenName: 'CheckuserId'
							}]
						}]
					}, {
						xtype: 'textfield',
						fieldLabel: '描述',
						name: 'Description',
						width: 420
					}]
				}]
			}],
			reader: new FSBS.FsbsQmStrc.JsonReader()
		});

		FSBS.FsbsQmStrc.StoreInPanel.superclass.initComponent.call(this);
	}
});

//FsbsQmStrc EditForm
FSBS.FsbsQmStrc.EditForm = Ext.extend(Infrastructure.BaseForm, {
	dataId: null,
	dataRecord: null,
	qmInOut: 'qmio_0', //汽贸 qmio_0 入库但 qmio_1 出库单
	bodyStyle: 'padding:0px 0px 0',
	submitUrl: '/FsbsQmStrc/SaveOrUpdate',

	initComponent: function () {
		var itemEditForm = this;
		var isEdit = this.dataRecord == null ? true : !this.dataRecord.data['QMState'];
		var qmio = this.dataRecord == null ? this.qmInOut : this.dataRecord.data['QMInout'];
		var ioTypeName = qmio == 'qmio_0' ? '入库' : '出库';
		this.menuPid = qmio == 'qmio_0' ? '0101' : '0102';
		/*
		var carStore = new Infrastructure.BaseJsonStore({
		QryUrl: '/FsbsQmStrc/GetCarPlist', //查询Url
		UpdUrl: '/FsbsQmStrc/SaveOrUpdateCarInfo', //更新记录Url
		DelUrl: '/FsbsQmStrc/DeleteCar?storeid=' + this.dataId, //删除记录Url
		fields: FSBS.FsbsQmCrif.BaseFieldsArray
		});

		carStore.setBaseParam('storeid', this.dataId);
		var carGrid = new FSBS.FsbsQmCrif.GridPanel({
		baseUrl: '/FsbsQmStrc/',
		//提交保存Url
		formSubmitUrl: '/FsbsQmStrc/SaveOrUpdateCarInfo?storeid=' + this.dataId,
		store: carStore,
		storeId: this.dataId,
		height: 400,
		tbar: [{
		xtype: 'tbaraddbtn', //添加入库车辆
		menuid: '010101',
		hidden: !(isEdit && qmio == 'qmio_0'),
		handler: function () {
		carGrid.onShowItemWin();
		}
		}, {
		xtype: 'tbardelbtn', //删除入库车辆
		menuid: '010101',
		hidden: !(isEdit && qmio == 'qmio_0'),
		handler: function () {
		carGrid.delEntity();
		}
		}, {
		xtype: 'tbaraddbtn', //添加出库车辆
		menuid: '010201',
		hidden: !(isEdit && qmio == 'qmio_1'),
		text: '添加出库车辆',
		handler: function () {
		carGrid.chooseItem();
		}
		}, {
		xtype: 'tbardelbtn', //删除出库车辆
		menuid: '010201',
		hidden: !(isEdit && qmio == 'qmio_1'),
		handler: function () {
		carGrid.delEntity();
		}
		}, {
		xtype: 'tbareditbtn', //编辑车辆信息
		menuid: '010101',
		hidden: true,
		handler: function () {
		carGrid.onShowItemWin(itemGrid.getSelectionModel().getSelected());
		}
		}],
		chooseItem: function () {
		var carStoreGrid = new FSBS.FsbsQmCrif.GridPanel({
		carState: '1',
		height: 500,
		tbar: [{
		xtype: 'tbaraddbtn',
		text: '选择',
		handler: function (e) {
		carStoreGrid.tbarSubmit('/FsbsQmStrc/StoreOutAddCars?StoreId=' + carGrid.storeId, '选定车辆添加到出库单吗?', '添加成功', 'afterChoose');
		}
		}],
		listeners: {
		'afterChoose': function (ele, rt) {
		if (rt) {
		carGrid.store.reload();
		}
		}
		}
		});
		var carStoreGridWin = new Infrastructure.BaseWindow({
		title: '请选择车辆',
		iconCls: 'car',
		width: 600,
		items: [carStoreGrid]
		});
		carStoreGridWin.show();
		carStoreGrid.store.load();
		}
		});
		*/
		var storePanel = new FSBS.FsbsQmStrc.StoreInPanel({
			dataId: this.dataId,
			qmInOut: qmio
		});

		Ext.applyIf(this, {
			items: [//carGrid, 
			storePanel],
			reader: new FSBS.FsbsQmStrc.JsonReader()
		});

		FSBS.FsbsQmStrc.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		//carStore.load();
	}
});

//FsbsQmStrc DetailWin
FSBS.FsbsQmStrc.EditWin = Ext.extend(Infrastructure.BaseWindow, {
	title: 'FSBS-汽贸',
	iconCls: 'car',
	bodyStyle: 'padding:5px 5px 0',
	width: 850,

	//constructor
	initComponent: function () {
		var carStoreForm = new FSBS.FsbsQmStrc.EditForm({
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						//var itemGridStore = itemGrid.store;
						//itemGridStore.reload();
						//JsHelper.ShowInfo('添加信息成功!');
					}
				}
			}
		});
		Ext.applyIf(this, {
			dataForm: carStoreForm
		});
		FSBS.FsbsQmStrc.EditWin.superclass.initComponent.call(this);
	}
});

